# -*- coding: utf-8 -*-
# ㊎ Author : XMZ
# ㊍ Time : 2021/5/3 16:25
# ㊌ File : RabbitMQ -> consumer_a.py
# ㊋ IDE :  PyCharm
# ㊏ 
import os
import sys
import time

import pika

self_path = os.path.dirname(os.path.abspath(__file__))
father_path = os.path.dirname(self_path)
parent_path = os.path.dirname(father_path)
sys.path.append(self_path)
sys.path.append(father_path)
sys.path.append(parent_path)

hosts = "10.0.2.22"
name = "admin"
pwd = "123456"

credential = pika.PlainCredentials(username=name, password=pwd)

connection = pika.BlockingConnection(
    pika.ConnectionParameters(
        host=hosts,
        credentials=credential,
        heartbeat=0,
    ))

channel = connection.channel()
queue_name = "TEST_QUEUE"
channel.queue_declare(queue=queue_name)


def rollback(ch, method, properties, body):
    print(f"从队列提取数据：{body.decode()},    休息时间：{len(body)}")
    time.sleep(0.2)
    ch.basic_ack(delivery_tag=method.delivery_tag)


# 设置公平调度
channel.basic_qos(prefetch_count=1)
# 设置监听队列，队列消息处理函数，确认机制，是否排他
channel.basic_consume(
    queue=queue_name,
    on_message_callback=rollback,
    auto_ack=False,
    exclusive=False,
)
# 启动并开始处理消息
channel.start_consuming()
